<?php
/**
 * Created by PhpStorm.
 * User: mabao
 * 消息：系统消息和聊天消息和聊天
 */
namespace app\home\controller;
use think\Db;
use think\Config;
class Jisuan extends Base
{
    /**
     * 计算器
     */
    public function homePage()
    {
        $data = Db::table('up_dk_ll')
            ->where('dl_type',1)
            ->select();
        $this->assign('data',$data);
        return $this->fetch ('jisuan/calculate');
    }
    /**
     * 计算器
     */
    public function getLls()
    {
        $data = Db::table('up_dk_ll')
            ->where('dl_type',input('type'))
            ->select();
        return json(['msg' => $data]);
    }
    /**
     * 计算
     */
    public function jisuanqi()
    {
        extract(input());
        $dkTotal = $dkTotal*10000;
        $dknl = $dknl/100;
        if($type == 1){ //1是等额本息，2是等额本金
            //贷款月数，20年就是240个月:(240)
            //贷款总额:(1000000)
            //贷款年利率:(0.049)
            $res = $this->debx($dkm,$dkTotal,$dknl);
        }else{
            $res = $this->debj($dkm,$dkTotal,$dknl);
        }
        $data = $res['data'];
        $alllixi = $res['alllixi'];
        return json(['status'=>200,'msg'=>$data,'alllixi'=>$alllixi]);
    }

    protected function debx($dkm,$dkTotal,$dknl)
    {
        $data = array();
        $emTotal = round($dkTotal * $dknl / 12 * pow(1 + $dknl / 12, $dkm) / (pow(1 + $dknl / 12, $dkm) - 1),2); //每月还款金额
        $lxTotal = 0; //总利息
        for ($i = 0; $i < $dkm; $i++) {
            $lx      = round($dkTotal * $dknl / 12,2);   //每月还款利息
            $em      = round($emTotal - $lx,2);  //每月还款本金
            $dkTotal = $dkTotal - $em > 0 ? $dkTotal - $em : 0;
            $data[$i]['num'] = $i + 1;
            $data[$i]['benjin'] = $em;
            $data[$i]['lixi'] = $lx;
            $data[$i]['month'] = $emTotal;
            $data[$i]['last'] = $dkTotal;
            //这是显示第几年--start
//            $data[$i]['year'] = '';
//            if($i % 12 == 0){
//                $data[$i]['year'] = '第' . ($i/12)+1 . '年';
//            }
            //这是显示第几年--end
            $lxTotal = $lxTotal + $lx;
        }
        $res['alllixi'] = $lxTotal;
        $res['data'] = $data;
        return $res;
    }
    protected function debj($dkm,$dkTotal,$dknl)
    {
        $data = array();
        $em      = round($dkTotal / $dkm,2); //每个月还款本金
        $lxTotal = 0; //总利息
        for ($i = 0; $i < $dkm; $i++) {
            $lx      = round($dkTotal * $dknl / 12,2); //每月还款利息
            $dkTotal = $dkTotal - $em > 0 ? $dkTotal - $em : 0;
            $data[$i]['num'] = $i + 1;
            $data[$i]['benjin'] = $em;
            $data[$i]['lixi'] = $lx;
            $data[$i]['month'] = $em + $lx;
            $data[$i]['last'] = $dkTotal;
            //这是显示第几年--start
//            $data[$i]['year'] = '';
//            if($i % 12 == 0){
//                $data[$i]['year'] = '第' . ($i/12)+1 . '年';
//            }
            //这是显示第几年--end
            $lxTotal = $lxTotal + $lx;
        }
        $res['alllixi'] = $lxTotal;
        $res['data'] = $data;
        return $res;

    }


}